<?php

/**
 * home actions.
 *
 * @package    sf_sandbox
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class homeActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
    }

    public function executeCountrySelect()
    {
    }

    public function executeLogin()
    {
        $this->getUser()->setCulture($this->getRequestParameter('lang'));
        $this->getUser()->setAttribute("lang", $this->getRequestParameter('lang'));

        $char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 2));

        // Concatenate the random string onto the random numbers
        // The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
        // '0' is left out to avoid confusion with 'O'
        $str = rand(1, 7) . rand(1, 7) . $char;
        $this->getUser()->setAttribute(Globals::SYSTEM_CAPTCHA_ID, $str);

        $c = new Criteria();
        $c->add(AppSettingPeer::SETTING_PARAMETER, Globals::SETTING_SERVER_MAINTAIN);
        $this->appSetting = AppSettingPeer::doSelectOne($c);
    }

    public function executeLogout()
    {
        $this->getUser()->getAttributeHolder()->clear();
        return $this->redirect('home/login');
    }

    public function executeDoLogin()
    {
        $str = "";
        if ($this->getRequestParameter('doAction') == "lang") {
            $c = new Criteria();
            $c->add(AppSettingPeer::SETTING_PARAMETER, Globals::SETTING_SERVER_MAINTAIN);
            $this->appSetting = AppSettingPeer::doSelectOne($c);

            $this->getUser()->setCulture($this->getRequestParameter('lang'));
            $this->username = $this->getRequestParameter('username');
            $this->userpassword = $this->getRequestParameter('userpassword');

            $this->setTemplate("login");
            //return $this->redirect('home/login');
        } else {
            $existUser = null;
            if (sfConfig::get('sf_environment') == Globals::SF_ENVIRONMENT_DEV && $this->getRequestParameter('username') == "" && $this->getRequestParameter('userpassword') == "") {
                // ******************* uncomment for testing purpose ****************
                $existUser = AppUserPeer::retrieveByPk(3);
            } else {
                $username = trim($this->getRequestParameter('username'));
                $password = trim($this->getRequestParameter('userpassword'));

                if ($username == '' || $password == '') {
                    $this->setFlash('errorMsg', "Invalid username or password.");
                    return $this->redirect('home/login?lang='.$this->getUser()->getAttribute("lang"));
                }

                //$this->getUser()->getAttributeHolder()->clear();
                $url = "http://id.ofxltd.com";
                if ($this->getUser()->getAttribute("lang") == "cn") {
                    $url = "http://cn.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "id") {
                    $url = "http://id.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "th") {
                    $url = "http://th.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "sg") {
                    $url = "http://sg.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "ph") {
                    $url = "http://ph.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "nz") {
                    $url = "http://nz.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "au") {
                    $url = "http://au.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "vn") {
                    $url = "http://vn.ofxltd.com";
                } else if ($this->getUser()->getAttribute("lang") == "my") {
                    $url = "http://my.ofxltd.com";
                }
                $url .= "/home/verifyExternalLogin?username=".$username."&userpassword=".$password;

                $str = file_get_contents($url);
                /*	    user      	*/
                //$array = explode(',', Globals::STATUS_ACTIVE . "," . Globals::STATUS_PENDING);
                //$c = new Criteria();
                //$c->add(AppUserPeer::USERNAME, $username);
                //$c->add(AppUserPeer::USERPASSWORD, $password);
                //$c->add(AppUserPeer::USER_ROLE, Globals::ROLE_DISTRIBUTOR);
                //$c->add(AppUserPeer::STATUS_CODE, $array, Criteria::IN);
                //$existUser = AppUserPeer::doSelectOne($c);
            }

            if ($str == "true") {
                /*$c = new Criteria();
                $c->add(MlmDistributorPeer::USER_ID, $existUser->getUserId());
                $existDist = MlmDistributorPeer::doSelectOne($c);*/

                /*$c = new Criteria();
                $c->add(MlmDistributorPeer::UPLINE_DIST_ID, $existDist->getDistributorId());
                $c->addAnd(MlmDistributorPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
                $distributors = MlmDistributorPeer::doSelect($c);

                if (count($distributors) > 0) {*/
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->addCredential(Globals::PROJECT_NAME . "DISTRIBUTOR");

                //$this->getUser()->setAttribute(Globals::SESSION_DISTID, $existDist->getDistributorId());
                //$this->getUser()->setAttribute(Globals::SESSION_USERID, $existUser->getUserId());
                //$this->getUser()->setAttribute(Globals::SESSION_USERNAME, $existUser->getUsername());
                //$this->getUser()->setAttribute(Globals::SESSION_NICKNAME, $existDist->getNickname());
                //$this->getUser()->setAttribute(Globals::SESSION_USERTYPE, $existUser->getUserRole());
                //$this->getUser()->setAttribute(Globals::SESSION_USERSTATUS, $existUser->getStatusCode());

                //$existUser->setLastLoginDatetime(date("Y/m/d h:i:s A"));
                //$existUser->save();

                $char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 2));

                // Concatenate the random string onto the random numbers
                // The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
                // '0' is left out to avoid confusion with 'O'
                $str = rand(1, 7) . rand(1, 7) . $char;
                //return $this->redirect('info/index?p='.$str."&lang=".$this->getUser()->getAttribute("lang"));
                return $this->redirect('main/index');
                //}
            }

            $this->setFlash('errorMsg', "Invalid username or password.");
            return $this->redirect('home/login?lang='.$this->getUser()->getAttribute("lang"));
        }

    }

    public function executeDoValidateLogin()
    {
        $str = "";
        $errorMsg = "";

        $existUser = null;
        $error = false;
        if (sfConfig::get('sf_environment') == Globals::SF_ENVIRONMENT_DEV && $this->getRequestParameter('username') == "" && $this->getRequestParameter('userpassword') == "") {
            // ******************* uncomment for testing purpose ****************
            $existUser = AppUserPeer::retrieveByPk(3);
        } else {
            $username = trim($this->getRequestParameter('username'));
            $password = trim($this->getRequestParameter('userpassword'));
            $lang = trim($this->getRequestParameter('lang'));

            if ($username == '' || $password == '') {
                $errorMsg = "Invalid username or password.";
                $error = true;
            } else {
                $url = "http://id.ofxltd.com";
                if ($lang == "cn") {
                    $url = "http://cn.ofxltd.com";
                } else if ($lang == "id") {
                    $url = "http://id.ofxltd.com";
                } else if ($lang == "th") {
                    $url = "http://th.ofxltd.com";
                } else if ($lang == "sg") {
                    $url = "http://sg.ofxltd.com";
                } else if ($lang == "ph") {
                    $url = "http://ph.ofxltd.com";
                } else if ($lang == "nz") {
                    $url = "http://nz.ofxltd.com";
                } else if ($lang == "au") {
                    $url = "http://au.ofxltd.com";
                } else if ($lang == "vn") {
                    $url = "http://vn.ofxltd.com";
                } else if ($lang == "my") {
                    $url = "http://my.ofxltd.com";
                }
                $memberAreaUrl = $url;
                $url .= "/home/verifyExternalLogin?username=".$username."&userpassword=".$password;

                $this->getUser()->setAttribute("MEMBER_URL", $memberAreaUrl);
                $str = file_get_contents($url);
            }
        }

        if ($str == "true") {
            $this->getUser()->setAuthenticated(true);
            $this->getUser()->addCredential(Globals::PROJECT_NAME . "DISTRIBUTOR");
            $char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 2));

            // Concatenate the random string onto the random numbers
            // The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
            // '0' is left out to avoid confusion with 'O'
            $str = rand(1, 7) . rand(1, 7) . $char;
        } else {
            $error = true;
            $errorMsg = "Invalid username or password.";
        }

        $arr = array(
            'error' => $error
            , 'errorMsg' => $errorMsg
        );
        echo json_encode($arr);
        return sfView::HEADER_ONLY;
    }

    public function executeDoValidateLogin2()
    {
        $str = "";
        $errorMsg = "";
        $error = false;

        $lang = trim($this->getRequestParameter('lang'));

        $url = "http://id.ofxltd.com";
        if ($lang == "cn") {
            $url = "http://cn.ofxltd.com";
        } else if ($lang == "id") {
            $url = "http://id.ofxltd.com";
        } else if ($lang == "th") {
            $url = "http://th.ofxltd.com";
        } else if ($lang == "sg") {
            $url = "http://sg.ofxltd.com";
        } else if ($lang == "ph") {
            $url = "http://ph.ofxltd.com";
        } else if ($lang == "nz") {
            $url = "http://nz.ofxltd.com";
        } else if ($lang == "au") {
            $url = "http://au.ofxltd.com";
        } else if ($lang == "vn") {
            $url = "http://vn.ofxltd.com";
        } else if ($lang == "my") {
            $url = "http://my.ofxltd.com";
        }
        $memberAreaUrl = $url;

        $this->getUser()->setAttribute("MEMBER_URL", $memberAreaUrl);
        $this->getUser()->setAuthenticated(true);
        $this->getUser()->addCredential(Globals::PROJECT_NAME . "DISTRIBUTOR");

        $arr = array(
            'error' => $error
            , 'errorMsg' => $errorMsg
        );
        echo json_encode($arr);
        return sfView::HEADER_ONLY;
    }

    public function executeLoginSecurity()
    {
        $this->setFlash('errorMsg', "Login required. This page is not public.");
        return $this->redirect('home/countrySelect');
    }

    public function executeLanguage()
    {
        $this->getUser()->setCulture($this->getRequestParameter('lang'));
        $this->redirect($this->getRequest()->getReferer());
    }

    public function executeUpdateMenuIdx()
    {
        $this->getUser()->setAttribute(Globals::SESSION_MENU_IDX, $this->getRequestParameter('menuIdx'));
        return sfView::HEADER_ONLY;
    }

    public function executeLoadDatatableLanguagePack()
    {
        if ($this->getUser()->getCulture() == "cn") {
            echo '{
                "sProcessing":   "处理中...",
                "sLengthMenu":   "显示 _MENU_ 项结果",
                "sZeroRecords":  "没有匹配结果",
                "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix":  "",
                "sSearch":       "搜索:",
                "sUrl":          "",
                "oPaginate": {
                    "sFirst":    "首页",
                    "sPrevious": "上页",
                    "sNext":     "下页",
                    "sLast":     "末页"
                }
            }';
        } else {
            echo '{}';
        }
        return sfView::HEADER_ONLY;
    }

    public function executeBillingReturn()
    {
        // read the post from PayPal system and add 'cmd'
        $req = 'cmd=_notify-synch';

        $tx_token = $this->getRequestParameter('tx');
        $auth_token = "WYODpzT9cUgnSmSmtREYlN1uZcNPRwxVRe3d4NPHfHKpKRLF8OxuJBwIqJ8";
        $req .= "&username=miracle&userpassword=echie881";

        $url = "http://id.ofxltd.com/home/verifyExternalLogin?username=miracle&userpassword=echie881";

        $str = file_get_contents($url);
        var_dump($str);
    }
}
